package com.exporter.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigurationLoader
{
    public static String HOST_IP = "";
    public static String HOST_PORT = "";
    public static String HOST_USER = "";
    public static String HOST_PASSWORD = "";
    public static String FOLDER_UPLOAD_PATH = "";
    public static String FOLDER_IGNORE = "";
    public static String FILE_IGNORE = "";
    
    public static boolean loadConfiguration()
    {
        File file = new File("exporter.properties");
        if(file.exists())
        {
            FileInputStream fis;
            try
            {
                fis = new FileInputStream( file );
                Properties properties = new Properties();
                properties.load( fis );
                String hostIP = (String)properties.get( "host.ip" );
                String hostPort = (String)properties.get( "host.port" );
                String hostUser = (String)properties.get( "host.user" );
                String hostPassword = (String)properties.get( "host.password" );
                String folderUploadPath = (String)properties.get( "file.upload.path" );
                String folderIgnore = (String)properties.get( "folder.ignore" );
                String fileIgnore = (String)properties.get( "file.ignore" );
                HOST_IP = hostIP;
                HOST_PORT = hostPort;
                HOST_USER = hostUser;
                HOST_PASSWORD = hostPassword;
                FOLDER_UPLOAD_PATH = folderUploadPath;
                FOLDER_IGNORE = folderIgnore;
                FILE_IGNORE = fileIgnore;
                fis.close();
            }
            catch ( FileNotFoundException e )
            {
                e.printStackTrace();
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
//          JOptionPane.showMessageDialog( this, "found " );
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static boolean storeConfiguration(Properties properties)
    {
        File file = new File("exporter.properties");
        try
        {
            FileOutputStream fos = new FileOutputStream( file );
            properties.store( fos, "Properties Files Updated" );
            fos.close();
        }
        catch ( FileNotFoundException e )
        {
            e.printStackTrace();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        return false;
    }
}
